//
// Created by Administrator on 2022/1/26.
//

#ifndef CPP_LEARNING_LEETCODE227_H
#define CPP_LEARNING_LEETCODE227_H
#include <vector>
using namespace std;
#include <math.h>
#include <iostream>
#include <deque>

//    5   4   3   2
//    1+4/2
//     sign1 sign2
bool shouldCalulateFirst(char sign1, char sign2){
    if(sign2 == '*' or sign2 == '/'){
        if(sign1 == '+' or sign1 == '-') {
            return true;
        }else{
            return false;
        }
    }else{
        return false;
    }
}

long operate(long op1, long op2, char sign){
    if(sign == '+'){
        return op1 + op2;
    }else if(sign == '-'){
        return op1 - op2;
    }else if(sign == '*'){
        return op1 * op2;
    }else if(sign == '/'){
        return op1 / op2;
    }
}

bool isValid(char c){
    return ('0'<= c && c<='9')  ||
            c == '+'            ||
            c == '-'            ||
            c == '/'            ||
            c == '*' ;
 }
class Solution227 {
public:

    void parseString(string& s, deque<char>& signQueue, deque<long>& numQueue){
        long num = 0;
        for(int i=0; i<s.size(); i++){
            if(! isValid(s[i]))   continue;

            if('0' <= s[i] &&  s[i]  <= '9'){
                num = num*10 + s[i] - '0';
            }else{
                numQueue.push_back(num);
                num = 0;
                signQueue.push_back(s[i]);
            }
        }
        numQueue.push_back(num);
    }
    int calculate(string s) {
        deque<char> signQueue;
        deque<long> numQueue;
        parseString(s, signQueue, numQueue);

        deque<char> signQueue2;
        deque<long> numQueue2;

        numQueue2.push_back(numQueue.front());
        numQueue.pop_front();

        while (!signQueue.empty()){
            char sign2 = signQueue.front();
            signQueue.pop_front();
            long op2 = numQueue.front();
            numQueue.pop_front();

            if((signQueue2.size() >= 1) && shouldCalulateFirst(signQueue2.back(), sign2)){
                long op1 = numQueue2.back();
                numQueue2.pop_back();

                long val = operate(op1, op2, sign2);
                numQueue2.push_back(val);
            }else{
                signQueue2.push_back(sign2);
                numQueue2.push_back(op2);
            }
        }

        while (!signQueue2.empty()){
            char sign = signQueue2.front();
            signQueue2.pop_front();

            long op1 = numQueue2.front();
            numQueue2.pop_front();
            long op2 = numQueue2.front();
            numQueue2.pop_front();

            long val = operate(op1, op2, sign);
            numQueue2.push_front(val);
        }
        return numQueue2.front();
    }
};

//    1+4/2
//     sign1 sign2

void test227(){
    Solution227 s;




    int result = s.calculate("1");
    std::cout<<result<<std::endl;

}
#endif //CPP_LEARNING_LEETCODE209_H
